Write a python program to print the following pattern
$ * * * $
* $ $ *
* $ *
* $ $ *
$ * * * $
Help me display this pattern.
Write a python program to print the following pattern
$ * * * $
* $ $ *
* $ *
* $ $ *
$ * * * $
Help me display this pattern.
I have a company management system and in this company we have 2 roles, MANAGER and EMPLOYEE so MANAGER can:
EMPLOYEE can:
Do I need create 2 classes for MANAGER and EMPLOYEE or create only 1 class USER and has 2 roles. And what is the best database design for this situation?
I am rewriting an old app and I am trying to use async to speed it up.
The old code was doing something like this:
var value1 = getValue("key1");
var value2 = getValue("key2");
var value3 = getValue("key3");
where the getValue function was managing its own cache in a dictionary, doing something like this:
object getValue(string key) {
if (cache.ContainsKey(key)) return cache[key];
var value = callSomeHttpEndPointsAndCalculateTheValue(key);
cache.Add(key, value);
return value;
}
If I make the getValue async and I await every call to getValue, then everything works well. But it is not faster than the old version because everything is running synchronously as it used to.
If I remove the await (well, if I postpone it, but that's not the focus of this question), I finally get the slow stuff to run in parallel. But if a second call to getValue("key1") is executed before the first call has finished, I end up with executing the same slow call twice and everything is slower than the old version, because it doesn't take advantage of the cache.
Is there something like await("key1") that will only await if a previous call with "key1" is still awaiting?
I have implemented state pattern for my socket library in Java. I simplified code to reduce size and increase readability just to demonstrate the problem. I don't know how to encapsulate using of Socket class by states classes. I've written comments to better understanding the problem. So, there is an example code:
package com.test;
enum ConnectionAttemptResult {
ConnectionAttemptStarted,
AlreadyInProgress,
AlreadyConnected,
UnableToCreateSocket,
UnableToConnectSocket,
UnableToCreateReadEvent
}
class SocketAddress {
static SocketAddress LocalhostPort(int port) {
// Code removed.
return new SocketAddress();
}
}
class Socket {
private State currentState;
Socket() {
this.currentState = new NotActiveState(this);
}
public ConnectionAttemptResult Connect(SocketAddress remoteAddress) {
return this.currentState.Connect(remoteAddress);
}
// Socket class is external and will be used by user.
// So further methods must be private because they are internal.
// Calling these methods by a user may broke class functionality.
// But I can't make them private because then states couldn't access them.
public void SwitchState(State newState) {
this.currentState = newState;
}
// I removed a lot of code of this class to simplify the code.
public boolean CreateSocket() {
// Code removed.
return true;
}
public boolean ConnectSocket(SocketAddress remoteAddress) {
// Code removed.
return true;
}
public boolean CreateReadEvent() {
// Code removed.
return true;
}
}
abstract class State {
protected Socket socket;
State(Socket socket) {
this.socket = socket;
}
public abstract ConnectionAttemptResult Connect(SocketAddress remoteAddress);
}
class NotActiveState extends State {
NotActiveState(Socket socket) {
super(socket);
}
@Override
public ConnectionAttemptResult Connect(SocketAddress remoteAddress) {
if (!socket.CreateSocket())
return ConnectionAttemptResult.UnableToCreateSocket;
if (!socket.ConnectSocket(remoteAddress))
return ConnectionAttemptResult.UnableToConnectSocket;
if (!socket.CreateReadEvent())
return ConnectionAttemptResult.UnableToCreateReadEvent;
socket.SwitchState(new ConnectingState(socket));
return ConnectionAttemptResult.ConnectionAttemptStarted;
}
}
class ConnectingState extends State {
ConnectingState(Socket socket) {
super(socket);
}
@Override
public ConnectionAttemptResult Connect(SocketAddress remoteAddress) {
return ConnectionAttemptResult.AlreadyInProgress;
}
}
class ConnectedState extends State {
ConnectedState(Socket socket) {
super(socket);
}
@Override
public ConnectionAttemptResult Connect(SocketAddress remoteAddress) {
return ConnectionAttemptResult.AlreadyConnected;
}
}
class Main {
public static void main(String[] args) {
Socket socket = new Socket();
socket.Connect(SocketAddress.LocalhostPort(9898));
// But I also can call internal methods and break the class functionality, because now they are public.
socket.CreateSocket();
socket.CreateReadEvent();
}
}
Where is my mistake? How usually developers implement this pattern saving encapsulation? Hope for your help! Thanks in advance!
I have been tasked with creating a library, which should be easily configurable and extendible by consuming clients.
suppose i have to create a library name testLibrary.. and it has three internal dependency.
class testLibrary {
private interfaceA a; //
private interfaceB b;
private interfaceC c;
constructor(interfaceA a, interfaceB b, interfaceC c){
initializes the class with default implementation of these interfaces.
}
}
here class C also depends on interfaceA , interfaceB
class C {
private interfaceA a;
private interfaceB b;
private interfaceD d;
constructor(interfaceA a, interfaceB b, interfaceD c){
//comes with a default implementation of interface A , B and D
}
}
Now if I am a client of class testLibrary and I want to use custom implementation of interfaceA and interfaceB inside this testLibrary, such that even internal dependencies of this library point to the custom ones if they depend on it.
What would be the best way to define this testLibrary so that its dependency can be easily pluggable ? Also Assume that as a client, i dont want to use any DI framework.
I would also be glad if people here can point me towards some good open source library which allow these kind of extensibility or pluggablity.
I created character movement (Player movement, root animation, player rotation, camera rotation) in one script. Of course that is not a good thing and of course i want to split up the code. But now I want to know how to make it better.
My thoughts was to seperate all logic:
and create CharacterPlayerController.cs, get references to all scripts and manipulate them in single Update().
So will it be better if I will tried to implement Mediator pattern? Or maybe use Visitor pattern?
In the future i want to implement IK system, Rig system, Climb system.
Maybe I'm thinking wrong and someone could tell me how to make it correct.
I have seen a pattern in code where you hide some of your virtual functions behind non virtual functions like this:
class IApa {
public:
virtual ~IApa() = default;
void update() {
doUpdate();
};
private:
virtual void doUpdate() = 0;
};
class Apa: public IApa {
private:
void doUpdate() override {...}
};
I know that it is some kind of code pattern, but to me it only looks like you pollute the interface. What is the reason for using a pattern like this?
It is also possible that the examples I have seen is implemented wrong, do you know any design pattern that is similar to this, and what is the motivation for doing it?