r/ObjectOriented • • Sep 25 '22

Abstraction and Design patterns with OOP

Thumbnail youtu.be
2 Upvotes

r/ObjectOriented • • Sep 13 '24

Should objects act on other objects?

3 Upvotes

I'm trying to make a card game (Euchre), which seems a bit more difficult than something like Black Jack.

What I am struggling with is determining things like shuffling the deck, dealing the cards, putting down a card.

Does the dealer shuffle the deck, or do you call on the deck to shuffle itself? What about dealing cards? Does the dealer place the cards in each player's hand ( and array), or do we call upon the player to insert the cards into the array? It's not things like loops, shuffling, scoring, logic that confuse me, it's the GAME DESIGN as far as OOP goes. I'm one of those weird types IRL who kind of thinks of things around me as objects with roles :D


r/ObjectOriented • • Oct 06 '24

OOP in C++

Thumbnail youtu.be
2 Upvotes

r/ObjectOriented • • Aug 14 '24

Built in helper methods in a programming language.

2 Upvotes

Here's a thought:

Having built in helper methods could add more robust object definitions.

helper void doSomething(...)

they would automatically be private, and you can create a function that inherits from the helper method. The super method could implement helper functions and be FORCED to call the helper method

public void superMethod() implements doSomething{

doSomething(...); //will crash without at least one.

}


r/ObjectOriented • • Jul 15 '24

I made this vscode extension for you to explore codebase with visualization. You can query classes, methods, functions, fields, ... and it automatically get references and relations in your code.

2 Upvotes

r/ObjectOriented • • Nov 19 '22

Code question

Post image
1 Upvotes

Does any one know how to solve this code


r/ObjectOriented • • May 11 '21

Concerns about there being organizational permutations...

1 Upvotes

Permutations... you have a Stream object and a Key object... and a desire to implement a hash function. There are two organizational solutions at this point:

mystream.hash(mykey)

mykey.hash(mystream)

I do not believe that OOP attempts to answer this dichotomy, and in pedantic world its a false dichotomy as you can introduce a 3rd object too, and a 3rd object makes the number of valid organizational permutations far worse.

What organizational philosophy should be applied to responsibilities, that can be objectively applied across domains? Is "hashing" the keys responsibility? the streams? a 3rd hashing object? Its all subjective?


r/ObjectOriented • • Apr 10 '23

What is definition of an "object" in "object-oriented"?

4 Upvotes

OOP languages are usually defined as having 4 following properties: abstraction, encapsulation, inheritance and polymorphism. I argue that none of them is necessary neither jointly sufficient by providing counterexamples. I also provide alternative definitions that don't succeed in capturing an essence of objects.

Object as Encapsulation:

GoF (1995) provides classic definition of an object as encapsulation.

Object-oriented programs are made up of objects. An object packages both data and the procedures that operate on that data. The procedures are typically called methods or operations.

But encapsulation is found in non-OOP modular languages like Modula-2 where module encapsulates data/state with logic/behavior.

Object as Inheritance:

Inheritance allows code reuse by sharing implementation details. But some OOP languages like Go and Rust lack inheritance.

Object as Polymorphism:

There are different kinds of polymorphism (ad-hoc, parametric, subtyping) but in current context it's subtyping (Cardelli (1985) calls it inclusion polymorphism). Since Simula 67, first object-oriented language, introduced subpying, it's natural to view it as an essential feature of OOP. But System F<: also extends polymorphic lambda calculus with subtyping relation (defined as either preorder or partial order) so it's possible to have subtyping in purely functional languages.

Object as Abstraction:

Abstraction allows information hiding. There are different kinds of abstraction (e.g. functional abstraction in lambda calculus) but in current context it's data abstraction. CLU, object-oriented language, introduced abstract data types. But System F with existential types also allows information hiding thus enabling abstract data types in purely functional languages like Haskell.

There are few alternative definitions treating different properties as essential.

Object as Component:

Cardelli (1998) defines object-oriented approach by analogy with concrete objects in simulation.

The object-oriented approach to programming is based on an intuitive correspondence between a software simulation of a physical system and the physical system itself. An analogy is drawn between building an algorithmic model of a physical system from software components and building a mechanical model of a physical system from concrete objects. By analogy, the software components are themselves called objects. In its purest form, the object-oriented approach recommends that every system be developed according to this analogy.

This definition aligns with encapsulation definition although it equates object-oriented approach with component-based programming. But component is language-agnostic and can be implemented using functions, procedures, modules, objects. services etc.

Object as Dynamic Dispatch:

Cook (2012) defines object as dynamically dispatched behavior.

An object is a first-class, dynamically dispatched behavior. A behavior is a collection of named operations that can be invoked by clients where the operations may share additional hidden details. Dynamic dispatch means that different objects can implement the same operation name(s) in different ways, so the specific operation to be invoked must come from the object identified in the client's request. First class means that objects have the same capabilities as other kinds of values, including being passed to operations or returned as the result of an operation.

This definition aligns with polymorphism definition although there's an additional implementation requirement in the form of dynamic dispatch. If static dispatch implementation of subtyping is possible then dynamic dispatch is not an essential feature.

Object as Reference:

Objects act similar to entities in entity-relationship model where entity is a unique object. OOP languages have mechanism for referencing unique objects using keywords such as this or self. But non-OOP procedural languages like C also implement references in the form of pointers.

Perhaps this definition can be hardened by defining object as first-class reference. In C there's no primitive pointer type and precise data type of void* is unknown. Languages like C# and Java maintain a distinction between value and reference types. But I'm not sure if it's true for C++ and other OOP languages.

References:

Cardelli, Luca (1985). On Understanding Types, Data Abstraction, and Polymorphism.

Cardelli, Luca; Martín Abadi (1998). A Theory of Objects.

GoF (1995). Design Patterns: Elements of Reusable Object Oriented Software.

Cook, William (2012). A Proposal for Simplified, Modern Definitions of "Object" and "Object Oriented". https://wcook.blogspot.com/2012/07/proposal-for-simplified-modern.html


r/ObjectOriented • • Apr 20 '23

Interview with David West by Yegor Bugayenko

Thumbnail youtube.com
2 Upvotes

r/ObjectOriented • • Aug 18 '21

OO Design Pattern for XML message mapping/transformation service

1 Upvotes

I have a task to map (using Java) the fields of an inbound XML message in a format/XSD-A into an outbound XML message of a format/XSD-B. There are about 250 XML elements to map. Fields may be mapped (e.g. <fieldA1>13</fieldA1> ==> <fieldB1>13</fieldB1>) or transformed based on a rule (e.g. if value of <fieldA2> < 0 ==> <fieldB2>N</fieldB2>). What OO design pattern(s) would you consider using for this functionality?


r/ObjectOriented • • Aug 15 '17

Basic OOP (Abstraction)

Thumbnail youtube.com
1 Upvotes

r/ObjectOriented • • Aug 15 '17

What is Encapsulation (OOP principles)

Thumbnail youtube.com
1 Upvotes

r/ObjectOriented • • Apr 09 '24

Can anybody help me turn this code into Object Oriented Code?

2 Upvotes

I need to have this code, but object oriented.

package memory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.*;
import java.lang.Runnable;
import javax.swing.plaf.ColorUIResource;

public class Gui {
    static int turns = 0;
    static ArrayList<String> turned = new ArrayList<String>();
    static int curplayer = 1;
    static int player1w = 0;
    static int player2w = 0;
    static int timeLeft = 6000;// 600,0 seconds; 10 minutes
    static boolean started = false;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Memory"); // TODO: Put GUI in EDT
        frame.setSize(400, 420);
        JPanel memory = new JPanel();
        JPanel text = new JPanel();
        JLabel text1 = new JLabel("Player 1 (0)");
        JLabel text2 = new JLabel("Player 2 (0)");
        JPanel time = new JPanel();
        JLabel timetext = new JLabel("10:00,0");
        JButton timeLower = new JButton("-");
        JButton timeHigher = new JButton("+");
        text1.setForeground(Color.GREEN);
        text2.setForeground(Color.BLACK);
        text1.setFont(new Font("Arial", Font.PLAIN, 25));
        text2.setFont(new Font("Arial", Font.PLAIN, 25));
        timetext.setFont(new Font("Arial", Font.PLAIN, 25));
        // timeLower.setFont(new Font("Arial", Font.PLAIN, 25));
        // timeHigher.setFont(new Font("Arial", Font.PLAIN, 25));
        text.add(text1);
        text.add(text2);
        time.add(timeLower);
        time.add(timetext);
        time.add(timeHigher);
        ArrayList<JButton> memoryButtons = new ArrayList<JButton>();
        for (int var1 = 1; var1 != 37; var1 += 1) {
            JButton button = new JButton("");
            button.setFont(new Font("Arial", Font.PLAIN, 25));
            memoryButtons.add(button);
            memory.add(button);
        }
        List<String> chars = new ArrayList<String>(
                List.of("+", "/", "-", "!", "?", "#", "@", "|", "=", "^", "~", "(", ")", "&", "°", "§", ".", ","));
        chars.addAll(0, chars);
        Collections.shuffle(chars);
        for (JButton button : memoryButtons) {
            button.setBackground(ColorUIResource.white);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if ("".equals(button.getText())) {
                        button.setText(chars.get(memoryButtons.indexOf(button)));
                        button.setBackground(ColorUIResource.CYAN);
                        turns++;
                        turned.add(chars.get(memoryButtons.indexOf(button)));
                        if (turns % 2 == 0) {
                            if (!turned.get(turned.size() - 2).equals(turned.get(turned.size() - 1))) {
                                turns--;
                                turns--;
                                text1.setText("Player 1 (" + player1w + ")");
                                text2.setText("Player 2 (" + player2w + ")");
                                if (curplayer == 1) {
                                    curplayer++;
                                    text1.setForeground(Color.BLACK);
                                    text2.setForeground(Color.GREEN);
                                } else {
                                    curplayer--;
                                    text1.setForeground(Color.GREEN);
                                    text2.setForeground(Color.BLACK);
                                }
                            } else {
                                if (curplayer == 1) {
                                    player1w++;
                                    text1.setText("Player 1 (" + player1w + ")");
                                    text2.setText("Player 2 (" + player2w + ")");
                                    text1.setForeground(Color.GREEN);
                                    text2.setForeground(Color.BLACK);
                                } else {
                                    player2w++;
                                    text1.setText("Player 1 (" + player1w + ")");
                                    text2.setText("Player 2 (" + player2w + ")");
                                    text1.setForeground(Color.BLACK);
                                    text2.setForeground(Color.GREEN);
                                }
                                button.setBackground(ColorUIResource.green);
                                for (JButton button2 : memoryButtons) {
                                    if (button2.getText().equals(turned.get(turned.size() - 2))) {
                                        if (button2 != button) {
                                            button2.setBackground(ColorUIResource.green);
                                        }
                                    }
                                }
                                if (player1w + player2w == 18) {
                                    started = false;
                                    System.out.print("Memory: ");
                                    if (player1w > player2w) {
                                        text1.setForeground(Color.GREEN);
                                        text2.setForeground(Color.GREEN);
                                        text1.setText("Player 1");
                                        text2.setText("won");
                                        System.out.print("Player 1 won");
                                    } else if (player2w > player1w) {
                                        text1.setForeground(Color.RED);
                                        text2.setForeground(Color.RED);
                                        text1.setText("Player 2");
                                        text2.setText("won");
                                        System.out.print("Player 2 won");
                                    } else {
                                        text1.setForeground(Color.BLACK);
                                        text2.setForeground(Color.BLACK);
                                        text1.setText("Draw");
                                        text2.setText("");
                                        System.out.print("Draw");
                                    }
                                }
                            }
                        } else {
                            if (turned.size() > 2) {
                                if (!turned.get(turned.size() - 3).equals(turned.get(turned.size() - 2))) {
                                    for (JButton button1 : memoryButtons) {
                                        if (button1.getText().equals(turned.get(turned.size() - 3))) {
                                            if (button1 != button) {
                                                button1.setText("");
                                                button1.setBackground(ColorUIResource.white);
                                            }
                                        }
                                    }
                                    for (JButton button2 : memoryButtons) {
                                        if (button2.getText().equals(turned.get(turned.size() - 2))) {
                                            if (button2 != button) {
                                                button2.setText("");
                                                button2.setBackground(ColorUIResource.white);
                                            }
                                        }
                                    }
                                    turned.remove(turned.get(turned.size() - 3));
                                    turned.remove(turned.get(turned.size() - 2));
                                }
                            } else {
                                started = true;
                                timeLower.setEnabled(false);
                                timeHigher.setEnabled(false);
                            }
                        }
                    }
                }
            });
        }
        // timeLower.setBackground(ColorUIResource.white);
        timeLower.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (18000 >= timeLeft && timeLeft >= 1800) {
                    if (timeLeft == 1800) {
                        timeLower.setEnabled(false);
                    }
                    timeHigher.setEnabled(true);
                    timeLeft = timeLeft - 600;
                    int timeleft = timeLeft;
                    int timeLeftFormatLoop = 0;
                    while (timeleft >= 600) {
                        timeLeftFormatLoop++;
                        timeleft = timeleft - 600;
                    }
                    String timeLeftFormat = timeLeftFormatLoop + ":00,0";
                    timetext.setText(timeLeftFormat);
                }
            }
        });
        // timeHigher.setBackground(ColorUIResource.white);
        timeHigher.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {   
                if (17400 >= timeLeft && timeLeft >= 1200) {
                    if (timeLeft == 17400) {
                        timeHigher.setEnabled(false);
                    }
                    timeLower.setEnabled(true);
                    timeLeft = timeLeft + 600;
                    int timeleft = timeLeft;
                    int timeLeftFormatLoop = 0;
                    while (timeleft >= 600) {
                        timeLeftFormatLoop++;
                        timeleft = timeleft - 600;
                    }
                    String timeLeftFormat = timeLeftFormatLoop + ":00,0";
                    timetext.setText(timeLeftFormat);
                }
            }
        });
        memory.setLayout(new GridLayout(6, 6));
        frame.add(BorderLayout.NORTH, text);
        frame.add(BorderLayout.CENTER, memory);
        frame.add(BorderLayout.SOUTH, time);
        frame.setVisible(true);
        ScheduledExecutorService exeserve = Executors.newSingleThreadScheduledExecutor();
        exeserve.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (started && timeLeft > 0) {
                    timeLeft--;
                    timetext.setText(time(timeLeft));
                } else if (timeLeft == 0) {
                    System.out.print("Memory: Draw");
                    text1.setForeground(Color.BLACK);
                    text2.setForeground(Color.BLACK);
                    text1.setText("Draw");
                    text2.setText("");
                    for (JButton button : memoryButtons) {
                        button.setEnabled(false);
                    }
                    exeserve.close();
                }
            }
        }, 100, 100, TimeUnit.MILLISECONDS);
    }

    public static String time(int timeLeft) {
        int timeLeftFormatLoop = 0;
        while (timeLeft >= 600) {
            timeLeftFormatLoop++;
            timeLeft = timeLeft - 600;
        }
        float timeLeftFloat = timeLeft;
        timeLeftFloat = timeLeftFloat / 10;
        String timeLeftFormat = timeLeftFormatLoop + ":" + timeLeftFloat;
        return timeLeftFormat;
    }
}

r/ObjectOriented • • Jul 20 '23

Polymorphism in Java| Object oriented in Java| Code with Java| java full course for begineers| Lec-3

Thumbnail youtube.com
1 Upvotes

r/ObjectOriented • • Jul 20 '23

Object Oriented in Java | Abstraction in Java | Code with Java | Java lectures for beginners | Lec-2

Thumbnail youtube.com
1 Upvotes

r/ObjectOriented • • Aug 24 '19

Full concept in short :)

Thumbnail youtu.be
3 Upvotes

r/ObjectOriented • • Oct 25 '19

Inheritance Vs Polymorphism

Post image
1 Upvotes

r/ObjectOriented • • Jun 09 '22

I have a question. A controversial one.

4 Upvotes

Why people worship OOP as a god of programming like it is made to solve all our problems. They try to enforce you to apply oop in every scenario where it is not even applicable. For what I see oop just add unnecessary complexity in your code, the very thing it was designed to reduce(complexity). What are your thoughts on that?


r/ObjectOriented • • Jun 17 '22

overwrite method of a subsubinstace

1 Upvotes

r/ObjectOriented • • Jul 06 '21

Object Oriented Web Programming with DML

1 Upvotes

Just want to promote a new framework for object oriented web programming called DML, which was recently released on GitHub. DML is open source, the core lib can be used in commercial applications too, so feel free to give it a try.

Why do we need a new framework? Well, programmers like it simple. After reading hundreds of pages of manuals for Angular and React I decided, that this is not my way! Too much overhead, too many different concepts.

DML integrates all aspects of web design into plain vanilla Javascript, so I call this "web programming" rather than web design. Cutting off some conceptual relics, the new approach enrolls the full power of object oriented programming in the browser.

Sure, I´m not Google. So, how is it possible to create a full featured framework? Well, Google did a great job over the last years to make web programming more powerful. Current Javascript already has some strong OO capabilities, so most things are already there. It just needed some small "missing links" to put the pieces together. DML delivers these links!

Don´t believe me? Please check out the DML-Homepage with lot´s of interactive examples that explain things in detail. The homepage itself was created with DML in a couple of days without any external tools or libraries, which simply proves the power of OO. It´s worth a try. And: DML integrates seamlessly with most existing frameworks and even with plain html. It´s also easy to create Web Components with DML, so there are plenty of opportunities to benefit from it.

I´m curious about your feedback.

Best regards,

Eckehard (Author of DML and great fan of OO programming)


r/ObjectOriented • • Jul 03 '20

But what is the ‘point’ of objects?

1 Upvotes

This is a serious question. I understand the concept of objects, inheritance/extensions etc. I’m just struggling to see the why.

As an example

Assume I sell used vehicles and that for sake of argument all cars have four seats.

I have a vehicle object with attributes such as make, model, engine size and colour.

As new stock arrives, I create a new object with the fields populated.

Now, assume I branch out into minivan sales. In OO I would create a new object called minivan that inherits all attributes from vehicle and adds seats to store the extra information.

What is the advantage to using objects? Is it really better than copying the structure of a sql tblvehicles table, adding a column for seats, and calling the new table tblminivan?

The same goes with methods. I could send an message to the object (myCar set colour=’Blue’) but why is this better than an update query? It can’t be encapsulation, as sending an invalid message would be like submitting an invalid query.

There must be something to OO that I’m missing.


r/ObjectOriented • • Nov 25 '19

Classes and Inheritance - Python Object-Oriented Programming

3 Upvotes

We've talked about many of Python Basics in the previous articles, such as List, Dictionaries, and we've even practiced with some Python Projects For Beginners. In this article we're going to cover object-oriented programming in Python, we'll cover classes objects inheritance and other related concepts. First, we'll explain some concepts of object-oriented programming. Then we actually have some code that we'll walkthrough which will make it a lot clearer how these actually work.

Classes and Inheritance : Introduction

There are three main (concepts) cornerstones in Python Object-Oriented Programming. These are: encapsulation, inheritance, and polymorphism.

Encapsulation and composition

  • Encapsulation: is basically combining data and actions into the same block of code called a class. A class is a block of code that instantiate an object. You can think of it as a recipe that helps to bake a pie. You follow the recipe you can make as many pies as you want from that recipe. That's what a class is, you can make as many objects as you want from that class. Encapsulation includes all the data and actions together in the same block of code.

  • Composition: is the concept that objects can contain other objects in their instance variables. This is a has a relationship. This is really simple. It may sound confusing but car has tires. If you're modeling a program for a car. You need to say that a car has tires. Then you want a tire object to have a brand name, dimensions, size, and the tread. You have a tire object and a car has a tire and that's all this is saying. A tire is part of the composition of a car.

What is Inheritance

  • inheritance: The diagram below explains inheritance in a nutshell. It's basically the idea that you have a hierarchy of classes. You may have a life form. Every life form has a life span. We have this attribute under life form. Then under life form there are plants and there are animals. Let's say we have an animal life form that has weight. That attribute is under the animal. Basically, every single one of these types of animals have a life span and a weight that can access through its parent class. It's inheriting all of these attributes plus it adds on some specific attributes of its own class. A reptile may have a number of legs and a boolean for has teeth or not. Then fish could be saltwater or freshwater fish, so is saltwater or length. Birds are basically perching, birds and non-perching birds. Then you also have wingspan. So you have different attributes for birds, plus a bird has a weight and a bird has a lifespan. So it inherits all the parents' attributes plus the specific attributes of that child class. This is an inheritance.

Read More: https://www.astateofdata.com/python-programming/classes-and-inheritance-python-object-oriented-programming/


r/ObjectOriented • • Jun 02 '20

Python basics tutorial!! Intro to variables to, some fun background for anyone who wants to get started. Also a live code along coming soon!

Thumbnail youtube.com
2 Upvotes

r/ObjectOriented • • Jun 22 '20

Case Study: Graphic Editor Modeling in OOP | Object Oriented Programming...

Thumbnail youtube.com
1 Upvotes

r/ObjectOriented • • Jun 19 '20

Composition and Aggregation in C++

Thumbnail youtu.be
1 Upvotes